package org.dc.activity.schedule;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import org.dc.R;
import org.dc.entity.Shift;
import org.dc.entity.WorkDay;
import org.dc.service.PersistenceManager;
import org.dc.service.WorkDayDao;
import org.dc.utlity.UIUtility;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 3/1/12
 * @time: 10:01 PM
 */
public class FillSchedule extends Activity {

    private List<Spinner> odd = new ArrayList<Spinner>();
    private List<Spinner> even = new ArrayList<Spinner>();
    private List<Spinner> always = new ArrayList<Spinner>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.create_schedule, null);
        setContentView(layout);

        TableLayout table = (TableLayout) layout.getChildAt(1);

        fillTable(PersistenceManager.getShiftDao(this).getShifts(), table);
        UIUtility.updateTableWidth(table, getWindowManager().getDefaultDisplay().getWidth() - 20);

        fillDatePickers((DatePicker) findViewById(R.id.fromSchedule),  (DatePicker) findViewById(R.id.tillSchedule));
    }

    private void fillDatePickers(DatePicker from, DatePicker till) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        from.init(year, month, 1, null);
        till.init(year, month, calendar.getActualMaximum(Calendar.DAY_OF_MONTH), null);
    }


    private void fillTable(final List<Shift> shifts, TableLayout table) {
        TableRow oddRow = (TableRow) (table).getChildAt(1);
        TableRow evenRow = (TableRow) (table).getChildAt(3);
        TableRow alwaysRow = (TableRow) (table).getChildAt(5);
        odd.clear();
        even.clear();
        always.clear();
        for (int i = 0; i < 7; i++) {
            Spinner oddSpinner = createSpinner(shifts);
            Spinner evenSpinner = createSpinner(shifts);
            Spinner alwaysSpinner = createSpinner(shifts);

            odd.add(oddSpinner);
            oddRow.addView(oddSpinner);


            even.add(evenSpinner);
            evenRow.addView(evenSpinner);

            always.add(alwaysSpinner);
            alwaysRow.addView(alwaysSpinner);
        }
    }

    private Spinner createSpinner(List<Shift> shifts) {
        Spinner spinner = new Spinner(this);
        List<Object> data = new ArrayList<Object>();

        data.add("");

        for (Shift shift : shifts) {
            data.add(shift);
        }

        ArrayAdapter simpleAdapter = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, data);
        simpleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(simpleAdapter);
        return spinner;
    }

    public void onOk(View v) {
        DatePicker from = (DatePicker) findViewById(R.id.fromSchedule);
        DatePicker till = (DatePicker) findViewById(R.id.tillSchedule);
        Calendar calendarFrom = Calendar.getInstance();
        calendarFrom.set(Calendar.YEAR, from.getYear());
        calendarFrom.set(Calendar.MONTH, from.getMonth());
        calendarFrom.set(Calendar.DAY_OF_MONTH, from.getDayOfMonth());

        Calendar calendarTill = Calendar.getInstance();
        calendarTill.set(Calendar.YEAR, till.getYear());
        calendarTill.set(Calendar.MONTH, till.getMonth());
        calendarTill.set(Calendar.DAY_OF_MONTH, till.getDayOfMonth());
        
        WorkDayDao workDayDao = PersistenceManager.getWorkDayDao(this);
        
        while(calendarFrom.before(calendarTill)){
            int day = calendarFrom.get(Calendar.DAY_OF_WEEK);
            int dayOfMonth = calendarFrom.get(Calendar.DAY_OF_MONTH);
            if(day == Calendar.SUNDAY){
                day = 6;
            } else {
                day -= 2;
            }

            Shift evenSchedule = getShift(even.get(day).getSelectedItem());
            Shift oddSchedule = getShift(odd.get(day).getSelectedItem());
            Shift alwaysSchedule = getShift(always.get(day).getSelectedItem());

            WorkDay workDay = new WorkDay();
            workDay.setDate(calendarFrom.getTime());

            if(alwaysSchedule != null){
                workDay.setShiftId(alwaysSchedule.getShiftId());
            }else {
                if(dayOfMonth%2 == 1 && oddSchedule != null){
                    workDay.setShiftId(oddSchedule.getShiftId());
                } else if (dayOfMonth%2 == 0 && evenSchedule != null){
                    workDay.setShiftId(evenSchedule.getShiftId());
                } else {
                    workDay.setShiftId(0);
                }
            }
            workDayDao.save(workDay);
            calendarFrom.add(Calendar.DAY_OF_YEAR, 1);
        }

        this.finish();
    }

    private Shift getShift(Object selectedItem) {
        if(selectedItem instanceof String){
            return  null;
        } 
        return (Shift) selectedItem;
    }

    public void onCancel(View v) {
        this.finish();
    }

}
